package club.itstudio.IContainer.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

public class CookieUtil {
    public static HashMap<String, String> getCookies() {
         ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null && attributes.getRequest().getCookies() != null) {
             HashMap<String, String> cookieMap = new HashMap<>(attributes.getRequest().getCookies().length);
             for (Cookie cookie : attributes.getRequest().getCookies()) {
                 cookieMap.put(cookie.getName(), cookie.getValue());
             }
             return cookieMap;
         }else {
             return new HashMap<>(0);
         }

    }
}
